using System;
using System.Web.Configuration;
using System.Xml;

namespace MyWebPagesStarterKit.Sitemaps
{
    public static class Sitemaps
    {
        private static System.Xml.Formatting _formatting;
        private static bool _initialized = false;
        private static readonly object _initLock = new object();
        private static bool _useFileModifiedDate;

        private static void Initialize()
        {
            if (!_initialized)
            {
                lock (_initLock)
                {
                    if (!_initialized)
                    {
                        SitemapSection webApplicationSection = (SitemapSection) WebConfigurationManager.GetWebApplicationSection("sitemaps");
                        if (webApplicationSection != null)
                        {
                            _formatting = webApplicationSection.Formatting;
                            _useFileModifiedDate = webApplicationSection.UseFileModifiedDate;
                        }
                        else
                        {
                            _formatting = System.Xml.Formatting.Indented;
                            _useFileModifiedDate = false;
                        }
                        _initialized = true;
                    }
                }
            }
        }

        public static System.Xml.Formatting Formatting
        {
            get
            {
                Initialize();
                return _formatting;
            }
        }

        public static bool UseFileModifiedDate
        {
            get
            {
                Initialize();
                return _useFileModifiedDate;
            }
        }
    }
}
